package com.wenx.activeMqCamel;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.DefaultMessage;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import static org.apache.activemq.camel.component.ActiveMQComponent.activeMQComponent;

public class activeMQProvider {
    public static void main(String[] args) throws Exception {
        CamelContext context = new DefaultCamelContext(); // 1. 创建 CamelContext.
        context.addComponent("activemq",activeMQComponent("tcp://10.10.3.186:61616"));
        context.setTracing(true);
        context.start(); // 4. 启动CamelContext.
        context.addRoutes((new activeMQProvider()).new test1());
        Thread.sleep(Integer.MAX_VALUE);  // 为了保持CamelContext处于工作状态，这里需要sleep主线程
        context.stop(); // 最后停止CamelContext
    }
    public class test1 extends RouteBuilder{
        @Override
        public void configure() throws Exception {
            double name = Math.random();
            from("quartz://825da6a0-7a77-49bf-8bbe-0301f0ee6957?cron=0 * * * * ? *&stateful=true")
                    .process(new Processor() {
                        @Override
                        public void process(Exchange exchange) throws Exception {
                            double name = Math.random();
                            exchange.getOut().setHeader(Exchange.HTTP_METHOD,"GET");
                            exchange.getOut().setHeader(Exchange.HTTP_QUERY,"id=123&name="+name+"");
                        }
                    })
                    .to("http://localhost:8081/demo/hello4")
                    //.setHeader("type",jsonPathExpression)
                    .process(new Processor() {
                        @Override
                        public void process(Exchange exchange) throws Exception {
                            //解析exchange中的返回值
                            DefaultMessage message = (DefaultMessage) exchange.getIn();
                            InputStream bodyStream =  (InputStream)message.getBody();
                            ByteArrayOutputStream result = new ByteArrayOutputStream();
                            byte[] buffer = new byte[1024];
                            int length;
                            while ((length = bodyStream.read(buffer)) != -1) {
                                result.write(buffer, 0, length);
                            }
                            //rest响应数据集
                            String bodystr = result.toString("UTF-8");
                            String data = "{\"data\":" + bodystr + "}";
                            result.close();
                            bodyStream.close();
                            System.out.println("bodystr="+bodystr);

//                            ReadContext context = JsonPath.parse(data);
//                            List<Map<String, Object>> mapList =
//                                    context.read("$.data[?((@.id > 500 && @.name == '王五') || @.id == 123)]");
//                            for(Map<String, Object> map : mapList){
//                                for(Map.Entry<String,Object> eap : map.entrySet()){
//                                    System.out.println("生产的值1: "+eap.getKey()+"  "+eap.getValue());
//                                }
//                            }
                            //JSONArray arrayJson = JSONArray.fromObject(mapList);
                            //JSONObject objectJson = JSONObject.fromObject(result1);
                           exchange.getOut().setBody(bodystr);
                        }
                    });
                    //.to("activemq:topic:test1");
        }
    }
}
